﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ttbit.core;

namespace ttbit.WorkingSpace
{

    class Parser_Texture : IWorkResourceParser
    {
        readonly string[] __exts = new string[] { ".jpg", ".png" };
        public string[] FileExt => __exts;

        public string TypeName => Resource_Texture.TypeStr;
        public override string ToString()
        {
            return TypeName + "=>" + __exts[0];
        }
        public bool CanCreate => false;
        public IWorkResource Load(bool reimport,System.IO.Stream metaStream, string path)
        {
            var name = Path.GetFileNameWithoutExtension(path).ToLower();

            var res = new Resource_Texture(name);
            //no need to read from meta.

            using (var fs = System.IO.File.OpenRead(path))
            {
                var t = CoreLoadTool.LoadTextureData_RGBA(fs, out int w, out int h);
                res.texture = new Texture((ushort)w, (ushort)h, ttbit.IGL.TextureType.RGBA_32, t);
            }
            return res;
        }
        public bool Save(IWorkResource resource,System.IO.Stream metaStream,string path)
        {
            //no need to write to meta.

            //no need to write to data;

            return false;
        }
        //导入,新建meta文件
        //public IWorkResource Import(ResourceMgr workspace, string path)
        //{
        //    var res = new Resource_Texture();
        //    var finfo = new FileInfo(path);

        //    res.meta.Init(this.TypeName, finfo);
        //    //如果有其它额外的数据在这里加保存代码

        //    using (var fs = System.IO.File.OpenRead(path))
        //    {
        //        var t = CoreLoadTool.LoadTextureData_RGBA(fs, out int w, out int h);
        //        res.texture = new Texture((ushort)w, (ushort)h, ttbit.IGL.TextureType.RGBA_32, t);
        //    }

        //    Save(workspace, res);
        //    return res;
        //}
        ////加载，优先用meta数据，检查原文件和meta是否匹配
        //public IWorkResource Load(ResourceMgr workspace, string path)
        //{
        //    var res = new Resource_Texture();
        //    var finfo = new FileInfo(path);

        //    //加载资源检查Meta是否变化
        //    bool bmetachange = res.meta.LoadAndCheck(this.TypeName, finfo);
        //    //如果有其它额外的数据在这里加保存代码

        //    using (var fs = System.IO.File.OpenRead(path))
        //    {
        //        var t = CoreLoadTool.LoadTextureData_RGBA(fs, out int w, out int h);
        //        res.texture = new Texture((ushort)w, (ushort)h, ttbit.IGL.TextureType.RGBA_32, t);
        //    }
        //    if (bmetachange)
        //    {
        //        Save(workspace, res);
        //    }
        //    return res;
        //}

        //public void Save(ResourceMgr workspace, IWorkResource resource)
        //{
        //    var outmetafile = resource.meta.srcfile+".meta";
        //    System.IO.File.Delete(outmetafile);
        //    using(var fs =System.IO.File.OpenWrite(outmetafile))
        //    {
        //        resource.meta.Write(fs);
        //        //如果有其它数据要写，自己在后面写
        //    }
        //}
    }
}
